<template>
  <div class="container">
    <div class="app-container">
      <div class="search">
        <el-form class="search-form">
          <el-form-item label="合作商搜索：" label-width="120px">
            <el-input
              v-model="pageParams.name"
              placeholder="请输入"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="search-btn"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="searchPartner"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="list">
        <div class="btns">
          <el-button
            type="primary"
            class="add-btn"
            icon="el-icon-circle-plus-outline"
            @click="addPartner"
            >新建</el-button
          >
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column width="80px" label="序号">
            <template v-slot="{ row }"> {{ row.index }} </template>
          </el-table-column>
          <el-table-column prop="name" label="合作商名称"> </el-table-column>
          <el-table-column prop="account" label="账号"> </el-table-column>
          <el-table-column prop="vmCount" label="设备数量"> </el-table-column>
          <el-table-column label="分成比例">
            <template v-slot="{ row }"
              ><span>{{ row.ratio }}%</span></template
            >
          </el-table-column>
          <el-table-column prop="contact" label="联系人"> </el-table-column>
          <el-table-column prop="account" label="联系电话"> </el-table-column>
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button type="text" @click="warning(row.id)"
                >重置密码</el-button
              >
              <el-button type="text" @click="getPartnerInfo(row)"
                >查看详情</el-button
              >
              <el-button type="text" @click="editPartnerInfo(row)"
                >修改</el-button
              >
              <el-button type="text" class="del" @click="delPartner(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="space-between" style="margin-top: 20px">
          <span class="pageCount"
            >共{{ pageParams.totalCount }}条记录 第{{ pageParams.pageIndex }}/{{
              pageParams.totalPage
            }}页</span
          >
          <el-pagination
            layout="slot"
            :current-page.sync="pageParams.pageIndex"
            :page-size="pageParams.pagesize"
            :total="pageParams.totalPage"
          >
            <el-button
              type="primary"
              :disabled="pageParams.pageIndex === 1 ? true : false"
              class="pagi-btn"
              :class="{ disabled: pageParams.pageIndex === 1 }"
              @click="goPrev"
              >上一页</el-button
            >
            <el-button
              type="primary"
              :disabled="
                pageParams.pageIndex === pageParams.totalPage ? true : false
              "
              :class="{
                disabled: pageParams.pageIndex === pageParams.totalPage,
              }"
              class="pagi-btn"
              @click="goNext"
              >下一页</el-button
            >
          </el-pagination>
        </el-row>
      </div>
      <!-- 弹窗 -->
      <!-- 添加 -->
      <addPartner
        v-if="showAddDialog"
        :title="title"
        :partnerInfo="partnerInfo || {}"
        @searchPartner="searchPartner"
        :isShow.sync="showAddDialog"
      />
      <!-- 查看详情 -->
      <el-dialog
        title="合作商详情"
        :visible="showInfoDialog"
        width="630px"
        @close="showInfoDialog = false"
      >
        <el-form ref="addForm" :model="partnerInfo">
          <el-row>
            <el-col :span="12">
              <el-form-item label="合作商名称：" label-width="150px">
                <span>{{ partnerInfo.name }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人:" label-width="150px" prop="remark">
                <span>{{ partnerInfo.contact }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话:" label-width="150px" prop="remark">
                <span>{{ partnerInfo.mobile }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分成比例:" label-width="150px" prop="remark">
                <span>{{ partnerInfo.ratio }}%</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
      <!-- 警告 -->
      <el-dialog
        :visible.sync="warningVisible"
        :show-close="false"
        append-to-body
        width="30%"
        class="warning"
      >
        <span class="warning-txt"
          ><i class="el-icon-warning" />确定要重置合作商密码吗？</span
        >
        <el-row type="flex" justify="center" style="margin-top: 40px">
          <el-col :span="5">
            <el-button size="small" @click="warningVisible = false"
              >取 消</el-button
            >
          </el-col>
          <el-col :span="4">
            <el-button
              size="small"
              class="confirm"
              type="primary"
              @click="resetPartnerPwd"
              >确 定</el-button
            ></el-col
          >
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addPartner from "@/views/node/components/addPartner.vue";
import {
  searchPartnerApi,
  delPartnerApi,
  resetPartnerPwdApi,
} from "@/api/node";
export default {
  name: "Region",
  components: {
    addPartner,
  },
  data() {
    return {
      status: [],
      tableData: [],
      title: "",
      partnerInfo: "",
      partnerId: "",
      showAddDialog: false,
      showSetDialog: false,
      showInfoDialog: false,
      warningVisible: false,
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        totalCount: 0,
        name: "",
      },
    };
  },
  created() {
    this.searchPartner();
  },
  methods: {
    // 搜索合作商
    async searchPartner() {
      const res = await searchPartnerApi({
        ...this.pageParams,
      });
      this.tableData = [...res.currentPageRecords];
      this.pageParams.pageIndex = +res.pageIndex;
      this.pageParams.pageSize = +res.pageSize;
      this.pageParams.totalPage = +res.totalPage;
      this.pageParams.totalCount = +res.totalCount;

      // 序号
      let pageNow = this.pageParams.pageIndex;
      this.tableData.forEach((item, index) => {
        this.$set(
          item,
          "index",
          pageNow > 1 ? (pageNow - 1) * 10 + index + 1 : index + 1
        );
      });
    },

    getPartnerInfo(data) {
      this.partnerInfo = data;
      this.showInfoDialog = true;
    },
    addPartner() {
      this.showAddDialog = true;
      this.title = "新增合作商";
    },
    editPartnerInfo(data) {
      this.showAddDialog = true;
      this.partnerInfo = data;
      this.title = "修改合作商";
    },
    async delPartner(id) {
      await delPartnerApi(id);
      this.$message.success("删除合作商成功");
      if (this.tableData.length === 1 && this.pageParams.pageIndex > 1)
        this.pageParams.pageIndex--;
      this.searchPartner();
    },
    warning(id) {
      this.partnerId = id;
      this.warningVisible = true;
    },
    async resetPartnerPwd() {
      await resetPartnerPwdApi(this.partnerId);
      this.$message.success("重置密码成功");
      this.warningVisible = false;
    },
    openAdd() {
      this.showAddDialog = true;
    },
    // 上一页
    goPrev() {
      this.pageParams.pageIndex--;
      this.searchPartner();
    },
    // 下一页
    goNext() {
      this.pageParams.pageIndex++;
      this.searchPartner();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #f8fafd;
  padding: 20px;
  .search {
    background-color: #fff;
    display: flex;
    height: 64px;
    align-items: center;
    padding-top: 23px;
    margin-bottom: 20px;
    .search-form {
      display: flex;
      align-items: center;
      .search-btn {
        background-color: #5f84ff;
        margin-left: 15px;
        font-size: 16px;
        height: 36px;
        width: 80px;
        text-align: center;
        padding: 0;
      }
    }
  }
  .list {
    // height: 673px;
    padding: 20px;
    background-color: #fff;

    .btns {
      margin-bottom: 15px;
    }
    .add-btn {
      width: 80px;
      text-align: center;
      padding: 12px 0px;
      background-color: #ff8035;
      border: none;
      font-size: 14px;
    }
    .setting-btn {
      border: none;
      background-color: #fbf4f0;
      color: #716762;
      width: 80px;
      text-align: center;
      padding: 12px 0px;
    }
    .pageCount {
      color: #dbdfe5;
      margin-left: 10px;
    }
  }
  .pagi-btn {
    background-color: #d5ddf8;
    color: #606266;
    width: 70px;
    height: 32px;
    text-align: center;
  }
  .disabled {
    background-color: #eceff8;
    color: #d2d7dd;
  }
  .del {
    color: #ff5a5a;
  }
}
.warning {
  margin-top: 150px;
  text-align: center;
  ::v-deep .el-dialog__body {
    padding-top: 10px;
  }
  .warning-txt {
    color: #000000;
    font-size: 16px;
  }
  .confirm {
    background-color: #7f9dff;
    border: 1px solid #7f9dff;
  }
  .el-icon-warning {
    color: #ff665f;
    font-size: 20px;
    margin-right: 10px;
  }
}
</style>
